﻿using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Microsoft.Office.Core;
using Microsoft.Office.Tools.Ribbon;
using eRechnung.Properties;


namespace eRechnung
{
    public partial class ebI_Ribbon : RibbonBase
    {
        private string _supertip = "Um {0} {1} klicken sie auf das Symbol.";
        private string _status = " Status: {0}";

        public ebI_Ribbon()
            : base(Globals.Factory.GetRibbonFactory())
        {
            InitializeComponent();

        }

        private void ebI_Ribbon_Load(object sender, RibbonUIEventArgs e)
        {
#if ebInterface4Bund
            btnSignAndMail.Visible = false;
            btnMailAndSave.Visible = false;
            btgZustellung.Visible = false;
            btnVerifySignature.Visible = false;
            btgZsEinstellg.Visible = false;
            btnEinstellgSignatur.Visible = false;
            btnEinstellgFinOnl.Visible = false;
            rbGrpSignSend.Visible = false; // no Upload in public
            BtnSignatur.Visible = false;
#else
            btnErbGvAt.Visible = false;
#endif
            if (Settings.Default.setHandyNr == "")
            {
                btnSignAndMail.Enabled = false; // wird erst freigegeben wenn Handy Einstellungen gespeichert wurden
            }

            // Setup Galleries
            // 1. Einstellungen
            var zs = ZustellSettings.LoadDefaults();


            SetGalleryItems(btgZsEinstellg, zs, true);

            // 2. Konfigurierte
            var zsConfig = ZustellSettings.LoadSettings();
            if (zsConfig.Count==0)
            {
                btgZustellung.Enabled = false;
            }
            else
            {
                SetGalleryItems(btgZustellung, zsConfig, false);
                btgZustellung.Enabled = true;
            }

#if ebInterface4Bund
            BtnDetails.Enabled = false;
#endif
        }

        private void SetGalleryItems(RibbonGallery pRbGallery, ZustellSettings pZs, bool pConfig)
        {
            pRbGallery.Items.Clear();
            var zsConfig = ZustellSettings.LoadSettings();
            foreach (var zustellSetting in pZs)
            {
                RibbonDropDownItem rbItem = this.Factory.CreateRibbonDropDownItem();
                string stNum = Enum.Format(typeof(ZustellSetting.Zustelldienst), zustellSetting.Value.Dienst, "d");
                rbItem.Image = zustellSetting.Value.Image;
                rbItem.Tag = zustellSetting.Value.Dienst;
                rbItem.Label = zustellSetting.Value.Dienst.ToString();
                rbItem.ScreenTip = "eZustellung" + ((pConfig)?" konfigurieren":" ausführen");
                bool validConf = false;
                if ((zsConfig != null))
                {
                    validConf = zsConfig.ContainsKey(zustellSetting.Key);
                }
                // string konf = string.Format(_status, (validConf) ? "konfiguriert" : "nicht konfiguriert");
                string sptip = string.Format(_supertip, rbItem.Label, ((pConfig) ? "zu konfigurieren" : "auszuwählen"));
                rbItem.SuperTip = sptip;
                pRbGallery.Items.Add(rbItem);
            }
            pRbGallery.ShowItemLabel = true;
            if (pRbGallery.Items.Count > 0)
            {
                pRbGallery.Enabled = true;
            }
            else
            {
                pRbGallery.Enabled = false;
            }
        }

        private void BtnDetails_Click(object sender, RibbonControlEventArgs e)
        {
#if ebInterface4Bund
            
            Form Details = new FrmDetailsBund();
#else
            Form Details = new FrmDetails();
#endif
            Details.Show();
        }

        private void BtnSkonto_Click(object sender, RibbonControlEventArgs e)
        {
            Form myForm = new FrmSkonto();
            myForm.Show();
        }

        private void BtnSave_Click(object sender, RibbonControlEventArgs e)
        {
            eRechnung.Globals.ThisDocument.InvXml.SaveXml(InvoiceXML.SaveMethodeTypes.SaveAsUnsignedInvoice);
        }

        private void BtnEditSettings_Click(object sender, RibbonControlEventArgs e)
        {
            FrmEbInterfaceSettings ebIsetting = new FrmEbInterfaceSettings();
            ebIsetting.Show();
        }

        private void BtnSaveAsTemplate_Click(object sender, RibbonControlEventArgs e)
        {
            eRechnung.Globals.ThisDocument.InvXml.SaveXml(InvoiceXML.SaveMethodeTypes.SaveAsTemplate);
        }

        private void BtnLoadTemplate_Click(object sender, RibbonControlEventArgs e)
        {
            eRechnung.Globals.ThisDocument.LoadTemplateXML(InvoiceXML.TemplateTypes.FromFile);
        }

        private void BtnEbInterface_Click(object sender, RibbonControlEventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.ebinterface.at");
        }

        private void BtnAustriaPro_Click(object sender, RibbonControlEventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.austriapro.at");
        }

        private void BtnSignatur_Click(object sender, RibbonControlEventArgs e)
        {
            // http://portal.wko.at/wk/format_detail.wk?AngID=1&StID=424389&DstID=940
            System.Diagnostics.Process.Start("http://portal.wko.at/wk/format_detail.wk?AngID=1&StID=424389&DstID=940");
        }

        private void btnErbGvAt_Click(object sender, RibbonControlEventArgs e)
        {
            System.Diagnostics.Process.Start("https://www.erb.gv.at");
        }


        private void BtnAbout_Click(object sender, RibbonControlEventArgs e)
        {
            Form AboutForm = new AboutForm();
            DialogResult dr = AboutForm.ShowDialog();
        }

        private void BtnHelp_Click(object sender, RibbonControlEventArgs e)
        {
            MessageBox.Show("Weitere Hilfe finden Sie im mitgelieferten Dokument 'Ausfüllhilfe.pdf'" +Environment.NewLine+"oder im Forum http://www.ebinterface.org.");
        }

        private void btnSignAndMail_Click(object sender, RibbonControlEventArgs e)
        {
            eRechnung.Globals.ThisDocument.InvXml.SaveXml(InvoiceXML.SaveMethodeTypes.MailSignedInvoice);
        }


        private void btnVerifySignature_Click(object sender, RibbonControlEventArgs e)
        {
            System.Diagnostics.Process.Start("https://pruefung.signatur.rtr.at/");
        }



        private void btnEinstellgSignatur_Click(object sender, RibbonControlEventArgs e)
        {
            FrmHandyEinstellungen HandyPrefs = new FrmHandyEinstellungen();
            HandyPrefs.ShowDialog();
        }

        private void btnNewInvoice_Click(object sender, RibbonControlEventArgs e)
        {
            DialogResult rc =
                MessageBox.Show(
                    "Sollen alle Datenfelder gelöscht werden und die Werte aus den Einstellungen neu übernommen werden?",
                    "Daten löschen", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
            if (rc != DialogResult.Yes)
            {
                return;
            }
            eRechnung.Globals.ThisDocument.LoadTemplateXML(InvoiceXML.TemplateTypes.Empty);
        }

        /// <summary>
        /// Setzt bei den Ribbon Buttons die Eigenschaft Enabeled
        /// </summary>
        /// <param name="enable">true oder false</param>
        public void SetRibbonButtonsEnable(bool enable)
        {
            Globals.Ribbons.ebI_Ribbon.BtnSave.Enabled = enable;
            Globals.Ribbons.ebI_Ribbon.BtnSkonto.Enabled = enable;
            Globals.Ribbons.ebI_Ribbon.BtnDetails.Enabled = enable;
        }

        private void btnEinstellgFinOnl_Click(object sender, RibbonControlEventArgs e)
        {
            FrmFinOnlSettings FOsetting = new FrmFinOnlSettings();
            FOsetting.Show();
        }

        private void btnUIDBestaetigung_Click(object sender, RibbonControlEventArgs e)
        {
            eRechnung.Globals.ThisDocument.UID_Bestaetigung();
        }


        private void BtnZustellungClick(object sender, RibbonControlEventArgs e)
        {
            var zustellSettings = ZustellSettings.LoadSettings();
            if (zustellSettings == null)
            {
                MessageBox.Show("Kein Zustelldienst konfiguriert", "Zustellung", MessageBoxButtons.OK,
                                MessageBoxIcon.Stop);
                return;
            }

            var rbItemClicked = (RibbonGallery)sender;
            var rbItemSelected = rbItemClicked.SelectedItem;
            var dienst = (ZustellSetting.Zustelldienst)rbItemSelected.Tag;
            // MessageBox.Show("You clicked " + dienst.ToString());
            var zs = zustellSettings[dienst];
            InvoiceXML.SaveMethodeTypes zsTyp = (zs.SignAlways == true)
                                                    ? InvoiceXML.SaveMethodeTypes.MailSignedInvoice
                                                    : InvoiceXML.SaveMethodeTypes.MailUnsignedInvoice;

            Globals.ThisDocument.InvXml.SelectedZustelldienst = zs;
            eRechnung.Globals.ThisDocument.InvXml.SaveXml(zsTyp);

        }

        private void btgZsEinstellg_Click(object sender, RibbonControlEventArgs e)
        {
            var rbItemClicked = (RibbonGallery)sender;
            var rbItemSelected = rbItemClicked.SelectedItem;
            var dienst = (ZustellSetting.Zustelldienst)rbItemSelected.Tag;
            // MessageBox.Show("You clicked " + dienst.ToString());
            var zs = ZustellSettings.LoadDefaults();
            var zsClicked = zs[dienst];
            var zsFrm = new FrmEzustlgSettings {zsClicked = zsClicked};

            var rc = zsFrm.ShowDialog();

            if (rc == DialogResult.Cancel)
            {
                return;
            }
        // Einstellungen neu laden und Gallery aufbauen
            var zsConfig = ZustellSettings.LoadSettings();

            SetGalleryItems(btgZustellung, zsConfig, false);
        }

        private void btnMailAndSave_Click(object sender, RibbonControlEventArgs e)
        {
            eRechnung.Globals.ThisDocument.InvXml.SaveXml(InvoiceXML.SaveMethodeTypes.MailUnsignedInvoice);

        }

        private void btnSupport_Click(object sender, RibbonControlEventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.ebinterface.org");

        }



    }
}
